﻿
Imports osi.input.constants
Imports osi.input.constants.key_board

Namespace constants
    Namespace key_board
        Public Enum action As Byte
            down = 0
            up
            press

            MAX = maxUInt8
        End Enum

        Public Enum mask As UInt16
            shift = 16
            control = 17
            alt = 18
            MAX = maxUInt16
        End Enum
    End Namespace
End Namespace

Namespace key_board_status
    Public Module _key_board_status
        Private ReadOnly _last_action() As Byte

        Private Sub case_poped(ByVal c As [case])
            assert(Not c Is Nothing)
            If c.category = category.key_board AndAlso
               c.action >= 0 AndAlso c.action < action.MAX AndAlso
               c.mask >= 0 AndAlso c.mask < mask.MAX Then
                _last_action(c.mask) = c.action
            End If
        End Sub

        Sub New()
            ReDim _last_action(mask.MAX - 1)
            memset(_last_action, action.MAX)
            AddHandler input.case_poped, AddressOf case_poped
        End Sub

        Public Function last_action(ByVal m As UInt16, ByRef a As Byte) As Boolean
            If m >= 0 AndAlso m < mask.MAX Then
                a = _last_action(m)
                Return a >= 0 AndAlso a < action.MAX
            Else
                Return False
            End If
        End Function

        Public Function down(ByVal i As mask) As Boolean
            Dim a As Byte = Nothing
            Return last_action(i, a) AndAlso a = action.down
        End Function

        Public Sub trigger()
            'trigger the Sub New() method
        End Sub
    End Module
End Namespace
